package com.csy.orm_ktorm

import com.csy.orm_ktorm.model.Twitter
import com.csy.orm_ktorm.model.Users
import com.csy.orm_ktorm.model.twitters
import com.csy.orm_ktorm.model.users
import org.junit.jupiter.api.Test
import org.ktorm.database.Database
import org.ktorm.dsl.*
import org.ktorm.entity.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class OrmKtormApplicationTests {
    @Autowired
    lateinit var database: Database

    // 单表插入测试耗时
    @Test
    fun insertUser1WTest() {
        val begin = System.currentTimeMillis()
        for (i in 0..9999) {
            database.insert(Users) {
                set(it.name, "Ktorm_1W$i")
                set(it.age, 21)
                set(it.email, "test@qq.com")
            }
        }
        val end = System.currentTimeMillis()
        println("Ktorm_insertUser1WTest 耗时:" + (end - begin))
    }

    @Test
    fun insertUser10WTest() {
        var time = 0L
        for (i in 0..2) {
            val begin = System.currentTimeMillis()
            for (j in 0..99999) {
                database.insert(Users) {
                    set(it.name, "Ktorm_10W$i")
                    set(it.age, 21)
                    set(it.email, "test@qq.com")
                }
            }
            val end = System.currentTimeMillis()
            time += (end - begin)
            println("Ktorm_insertUser10WTest 耗时:" + (end - begin))
        }
        println("Ktorm_insertUser10WTest 平均耗时:" + time / 3)
    }

    @Test
    fun insertUser50WTest() {
        var time = 0L
        for (i in 0..2) {
            val begin = System.currentTimeMillis()
            for (j in 0..499999) {
                database.insert(Users) {
                    set(it.name, "Ktorm_50W$i")
                    set(it.age, 21)
                    set(it.email, "test@qq.com")
                }
            }
            val end = System.currentTimeMillis()
            time += (end - begin)

            println("Ktorm_insertUser50WTest 耗时:" + (end - begin))
        }
        println("Ktorm_insertUser50WTest 平均耗时:" + time / 3)
    }

    @Test
    fun findUserById1WTest() {
        val begin = System.currentTimeMillis()
        for (i in 0..9999L) {
            database.users.find { it.id eq i }
        }
        val end = System.currentTimeMillis()
        println("Ktorm_findUserById1WTest 耗时:" + (end - begin))
    }

    @Test
    fun findUserById10WTest() {
        val begin = System.currentTimeMillis()
        for (i in 0..99999L) {
            database.users.find { it.id eq i }
        }
        val end = System.currentTimeMillis()
        println("Ktorm_findUserById10WTest 耗时:" + (end - begin))
    }

    @Test
    fun findUserById100WTest() {
        val begin = System.currentTimeMillis()
        for (i in 0..999999L) {
            database.users.find { it.id eq i }
        }
        val end = System.currentTimeMillis()
        println("Ktorm_findUserById100WTest 耗时:" + (end - begin))
    }

    @Test
    fun findUserTeitterByUserId1WTest() {
        var time: Long = 0
        for (j in 0..2) {
            val begin = System.currentTimeMillis()
            for (i in 0..999999L) {
                val re = database.twitters.filter { it.userId eq 64230 }.toList()
            }
            val end = System.currentTimeMillis()
            time += end - begin
            println("findUserTeitterByUserId1WTest 耗时:" + (end - begin))
        }
        println("findUserTeitterByUserId1WTest 平均耗时:" + time / 3)
    }

    @Test
    fun findUserByPageTest() {
        val begin = System.currentTimeMillis()
        val db = database.users.filter { it.id eq 110001 }
            .drop(0)
            .take(1000)
        val end = System.currentTimeMillis()
        println("Ktorm_findUserByPageTest 耗时:" + (end - begin))
    }
}
